package handlers

import (
    "context"
    "log"

    "google.golang.org/grpc/codes"
    "google.golang.org/grpc/status"
    pb "your_project/proto"
    "your_project/battle"
)

// BattleHandler 处理战斗相关请求
type BattleHandler struct {
    pb.UnimplementedBattleServiceServer
}

// NewBattleHandler 创建战斗处理器实例
func NewBattleHandler() *BattleHandler {
    return &BattleHandler{}
}

// UseSkill 处理技能使用请求
func (h *BattleHandler) UseSkill(ctx context.Context, req *pb.UseSkillRequest) (*pb.UseSkillResponse, error) {
    log.Printf("Player %d using skill %d", req.PlayerId, req.SkillId)
    
    // 调用战斗系统处理技能使用
    result, err := battle.GetBattleManager().UseSkill(req.PlayerId, req.SkillId, req.TargetId, req.TargetPosition)
    if err != nil {
        return nil, status.Errorf(codes.Internal, "Failed to use skill: %v", err)
    }
    
    return &pb.UseSkillResponse{
        Success:         result.Success,
        Damage:          result.Damage,
        Effects:         convertToStatusEffectProtos(result.Effects),
        CasterPosition:  result.CasterPosition,
        TargetPosition:  result.TargetPosition,
    }, nil
}

// 将状态效果转换为Proto格式
func convertToStatusEffectProtos(effects []*battle.StatusEffect) []*pb.StatusEffect {
    protoEffects := make([]*pb.StatusEffect, 0, len(effects))
    
    for _, effect := range effects {
        protoEffects = append(protoEffects, &pb.StatusEffect{
            EffectId: effect.ID,
            Name:     effect.Name,
            Duration: effect.Duration,
            Value:    effect.Value,
        })
    }
    
    return protoEffects
}

// EnterBattle 处理玩家进入战斗
func (h *BattleHandler) EnterBattle(ctx context.Context, req *pb.EnterBattleRequest) (*pb.EnterBattleResponse, error) {
    log.Printf("Player %d entering battle type %d", req.PlayerId, req.BattleType)
    
    // 调用战斗系统处理进入战斗
    battleID, position, err := battle.GetBattleManager().EnterBattle(req.PlayerId, req.BattleType, req.SceneId)
    if err != nil {
        return nil, status.Errorf(codes.Internal, "Failed to enter battle: %v", err)
    }
    
    return &pb.EnterBattleResponse{
        Success:    true,
        Message:    "Entered battle successfully",
        BattleId:   battleID,
        Position:   position,
    }, nil
}

// LeaveBattle 处理玩家离开战斗
func (h *BattleHandler) LeaveBattle(ctx context.Context, req *pb.LeaveBattleRequest) (*pb.LeaveBattleResponse, error) {
    log.Printf("Player %d leaving battle %d", req.PlayerId, req.BattleId)
    
    // 调用战斗系统处理离开战斗
    err := battle.GetBattleManager().LeaveBattle(req.PlayerId, req.BattleId)
    if err != nil {
        return nil, status.Errorf(codes.Internal, "Failed to leave battle: %v", err)
    }
    
    return &pb.LeaveBattleResponse{
        Success:    true,
        Message:    "Left battle successfully",
    }, nil
}

// GetBattleStatus 获取战斗状态
func (h *BattleHandler) GetBattleStatus(ctx context.Context, req *pb.GetBattleStatusRequest) (*pb.GetBattleStatusResponse, error) {
    // 调用战斗系统获取战斗状态
    isInBattle, err := battle.GetBattleManager().GetBattleStatus(req.PlayerId, req.BattleId)
    if err != nil {
        return nil, status.Errorf(codes.Internal, "Failed to get battle status: %v", err)
    }
    
    return &pb.GetBattleStatusResponse{
        Success:      true,
        Message:      "Battle status retrieved successfully",
        IsInBattle:   isInBattle,
        // 可以在这里添加更多战斗状态信息
    }, nil
}    